//app.js

App({

  onLaunch: function () {
    var that = this
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    var cartStorage = wx.getStorageSync('cartStorage') || []
    wx.setStorageSync('cartStorage', cartStorage)
  },

  login: function (callback) {
    var that = this
    var domain = that.globalData.domain
    var loginUrl = that.globalData.PageInterface
    var fromid = callback.share_id
    wx.login({
      success: function (res) {
        wx.getUserInfo({
          success: function (user_info) {
            that.globalData.userInfo = user_info.userInfo
            wx.request({
              url: domain + loginUrl,
              method: 'post',
              header: { 'content-type': 'application/x-www-form-urlencoded' },
              data: {
                api: 10001,
                format_type: 'json',
                api_cate: 'LoginWEIXN',
                model: "MINI",
                code: res.code,
                encryptedData: user_info.encryptedData,
                iv: user_info.iv,
                fromid: fromid
              },
              success: function (data) {
                if (data.data===''){
                  return;
                }
                var jsonObj = JSON.parse(data.data.trim())
                if (jsonObj.status == 1) {
                  if (jsonObj.msg) {
                    wx.showToast({
                      title: jsonObj.msg,
                      icon: 'loading',
                      duration: 2000
                    })
                  } else if (jsonObj.data) {
                    try {
                      wx.setStorageSync('user_id', jsonObj.data.user_id)
                      wx.setStorageSync('nickName', jsonObj.data.nickName)
                      wx.setStorageSync('userAvatar', jsonObj.data.headimgurl)
                      wx.removeStorageSync('share_id')
                    } catch (e) {

                    }
                    that.globalData.inv_i = jsonObj.data.user_id
                  }
                }
              }
            })
          },fail:function(fail){
            wx.navigateBack({});
          }
        })
      },
      fail: res => {
        console.log(res)
      }
    })
  },

  readyCallback: function () {

  },

  globalData: {
    userInfo: null,
    awardsConfig: {},
    runDegs: 0,
    isAuthorize: true,
    inv_i: wx.getStorageSync('user_id') ? wx.getStorageSync('user_id') : '',
    background_color: '#37bda2',
    domain: 'https://www.chujiekeji.com/',
    PageInterface: 'Api/Page/index',
    // 地址信息
    addrInfoInterface: 'Api/Page/index',
    // 地址修改
    addrUpdateInterface: '/Api/Personal/BackAddress',
    // 地址删除
    addrDelInterface: 'Api/Personal/DelAddress',
    // 订单列表
    orderListInterface: 'Api/Page/index',
    // 订单详情
    orderDetailInterface: 'Api/Page/index',
    // 商品详情
    goodsDetailInterface: 'Api/Page/index',
    // 首页轮播图
    indexBannnerInterface: 'Api/Banner/BannerInfo',
    // 直播
    CurrInterface: 'Api/Curr/CurrInfo',
    // 商品
    GoodsInterface: 'Api/Goods/GoodsInfo',
    // 直播商品
    CurrGoodsInterface: 'Api/Goods/CurrGoods',
    // 商品分类
    GoodsCateInterface: 'Api/Goods/GoodsCate',
    // 商品详情
    goodsDetailInterface: 'Api/Goods/GoodsInfo',
    // 直播间
    LiveRoomInterface: 'Api/Live/LiveCreate',
    // 新闻中心
    NewInterface: 'Api/News/NewsInfo',
  }
})